package so.shanku.essential.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.Html;
import android.view.View;
import android.webkit.WebView;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.lidroid.xutils.ViewUtils;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.umeng.socialize.bean.SocializeConfig;
import com.umeng.socialize.sso.UMSsoHandler;
import net.tsz.afinal.annotation.view.ViewInject;
import net.tsz.afinal.json.JsonMap;
import net.tsz.afinal.json.JsonParseHelper;
import so.shanku.essential.R;
import so.shanku.essential.activity.base.BaseActivity;
import so.shanku.essential.adapter.RelationProductAdapter;
import so.shanku.essential.app.MyApplication;
import so.shanku.essential.getdata.*;
import so.shanku.essential.utils.*;
import striveen.util.used.view.view.gridview.PLA_AbsListView;
import striveen.util.used.view.view.gridview.PLA_AdapterView;
import striveen.util.used.view.view.gridview.XGridView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Jaelyn on 2015/9/16 0016.
 * 商城主题
 */
public class SpecialSubjectPromotionActivity extends BaseActivity {
    private String vendorId; //店铺的id

    /**
     * 顶部布局
     */
    @ViewInject(id = R.id.title_layout)
    private RelativeLayout title_layout;//
    /**
     * head view
     */
    @ViewInject(id = R.id.all_layout_back, click = "back_iv_click")
    private ImageView back_iv;//
    @ViewInject(id = R.id.left_menu_iv, click = "filter_iv_clcik")
    private ImageView filter_iv;//
    @ViewInject(id = R.id.head_title)
    private TextView head_title;//

    @ViewInject(id = R.id.search_iv, click = "search_iv_click")
    private ImageView search_iv;//
    @ViewInject(id = R.id.shoppingCart_iv, click = "shoppingCart_iv_click")
    private ImageView shoppingCart_iv;//
    @ViewInject(id = R.id.more_iv, click = "more_iv_click")
    private ImageView more_iv;//


    /**
     * head view
     */
    @ViewInject(id = R.id.shop_bg_iv)
    private ImageView shop_bg_iv;//主题图片
    @ViewInject(id = R.id.shop_name_tv)
    private TextView shop_name_tv;//主题
    @ViewInject(id = R.id.shop_discri_tv)
    private TextView shop_discri_tv;//主题描述

    @ViewInject(id=R.id.shop_theme_promotion_layout)
    private RelativeLayout shop_theme_promotion_layout;

    @ViewInject(id = R.id.promotion_time_tv)
    private TextView promotion_time_tv;//活动时间
    @ViewInject(id = R.id.promotion_appointment_tv,click = "promotion_appointment_tv_click")
    private TextView promotion_appointment_tv;//活动时间

    @ViewInject(id = R.id.webView)
    private WebView webView;//活动时间
    private JsonMap brandDatas; //店铺信息

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.shop_promotion_layout);
        filter_iv.setVisibility(View.GONE);
        vendorId = getIntent().getStringExtra(Constant.ID);

        getThemeDetail();
    }

    /**
     * 获取店铺详情    {VendorId:12,UserInfoId:123}
     */
    private void getThemeDetail() {
        loadingToast.show();
        Map<String, Object> params = new HashMap<String, Object>();

        if (Utils.isHasLogin(this)) {
            params.put("UserInfoId", Utils.getUserId(this));
        }
        params.put("VendorId", vendorId);
        GetDataQueue queue = new GetDataQueue();
        queue.setActionName(GetDataConfing.GetAction_GetVendorDetails);
        queue.setParamsNoJson(params);
        queue.setCallBack(callBack);
        queue.setWhat(GetDataConfing.What_GetVendorDetails);
        getDataUtil.getData(queue);
    }

    private IGetServicesDataCallBack callBack = new IGetServicesDataCallBack() {

        @Override
        public void onLoaded(GetDataQueue entity) {
            if (entity.isOk()) {
                if (ShowGetDataError.isCodeSuccess(mContext, entity.getInfo())) {
                    if (entity.what == GetDataConfing.What_GetVendorDetails) {
                        try {
                            JsonMap goodsDatas = JsonParseHelper.getJsonMap_List_JsonMap(entity.getInfo(), JsonKeys.Key_Info).get(0);
                            refreshShopDetail(goodsDatas);
                        } catch (Exception e) {
                            e.printStackTrace();
                            toast.showToast(R.string.server_error);
                            finish();
                        }

                    } else if (entity.what == GetDataConfing.WHAT_GET_FOCUS_ON) { //关注平拍
                        JsonMap<String, Object> data = JsonParseHelper
                                .getJsonMap(entity.getInfo());
                        try {
                            String msg = data.getString(JsonKeys.Key_Msg);
                            JsonMap temp = data.getList_JsonMap(JsonKeys.Key_Info).get(0);
                            String vendorId = temp.getStringNoNull("VendorId");
                            //toast.showToast(msg);

                            if (temp.getBoolean("IsFocus")) {
                                updateAttentionStatues(vendorId, true);
                            } else {
                                //removeFavourtePrograme(programId);
                                //jiemu_collect.setImageResource(R.drawable.icon_collect_no);
                                updateAttentionStatues(vendorId, false);
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }
            } else {

                ShowGetDataError.showNetError(mContext);
            }
            loadingToast.dismiss();
        }

    };

    /**
     * 设置主题信息 并请求相关商品的信息
     *
     * @param goodsDatas
     */
    private void refreshShopDetail(JsonMap goodsDatas) {
        brandDatas = goodsDatas;

        String backImge = goodsDatas.getStringNoNull("BackGroundImage");   //背景大图
        if (MyApplication.getInstance().getBitmapHashMap().containsKey(backImge)) {
            shop_bg_iv.setImageBitmap(MyApplication.getInstance().getBitmapHashMap().get(backImge));
        } else {
            ImageLoader.getInstance().displayImage(backImge, shop_bg_iv, MyApplication.getInstance().getShopBgOption(), new ImageLoaderBitmapCallBack());
        }

        boolean isFocus = goodsDatas.getBoolean("isCollection");

        shop_name_tv.setText(goodsDatas.getStringNoNull("BrandName"));
        //shop_discri_tv.setText("\t\t\t\t"+goodsDatas.getStringNoNull("VendorInfo"));
        webView.loadUrl(goodsDatas.getStringNoNull("VendorInfo"));
    }

    /**
     * 预约
     * @param view
     */
    public void promotion_appointment_tv_click(View view){

    }

    public void back_iv_click(View view) {
        finish();
    }

    /**
     * 分享的点击事件
     *
     * @param view
     */
    public void share_iv_click(View view) {
        JsonMap jsonMap_temp = brandDatas;
        String shareLogo = jsonMap_temp.getStringNoNull("brandLogo");
        String shareTitle = jsonMap_temp.getStringNoNull("ShopName");
        String shareContent = jsonMap_temp.getStringNoNull("VendorInfo");
        String shareUrl = "ShopMall/Index?vid=" + vendorId;

        MyApplication.getInstance().showShareCompont(mContext, shareLogo, shareUrl, shareTitle, shareContent);
    }

    /**
     * 筛选
     *
     * @param view
     */
    public void filter_iv_clcik(View view) {

    }

    /**
     * 收藏的点击事件
     *
     * @param view
     */
    public void collect_tv_click(View view) {
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        /**使用SSO授权必须添加如下代码 */

//		UMSocialService umSocialService=MyApplication.getInstance().getmController();
//		if(umSocialService!=null) {
//			UMSsoHandler ssoHandler =umSocialService.getConfig().getSsoHandler(requestCode);
//			if (ssoHandler != null) {
//				ssoHandler.authorizeCallBack(requestCode, resultCode, data);
//			}
//		}
        UMSsoHandler ssoHandler = SocializeConfig.getSocializeConfig().getSsoHandler(requestCode);
        if (ssoHandler != null) {
            ssoHandler.authorizeCallBack(requestCode, resultCode, data);
        }
    }

}